package com.cook.controller.user;

import com.cook.dto.user.UserDTO;
import com.cook.result.Result;
import com.cook.service.UserService;
import com.cook.vo.admin.AllUserDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户信息
     */
    @GetMapping("/{userId}")
    public Result<AllUserDetailVO> getUserInfoById(@PathVariable Long userId) {
        log.info("用户查询个人信息: userId={}", userId);

        try {
            AllUserDetailVO userInfo = userService.getUserInfoById(userId);
            return Result.success(userInfo);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询用户信息失败: ", e);
            return Result.error(500, "查询用户信息失败");
        }
    }

    /**
     * 更新用户信息
     */
    @PutMapping
    public Result<Boolean> updateUser(@RequestBody UserDTO userDTO) {
        log.info("更新用户信息: userId={}", userDTO.getUserId());

        try {
            boolean success = userService.updateUser(userDTO);
            if (success) {
                return Result.success("用户信息更新成功", true);
            } else {
                return Result.error(500, "用户信息更新失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("更新用户信息失败: ", e);
            return Result.error(500, "更新用户信息失败");
        }
    }


}
